deferred class CD_COMPLEX_CLIPPING_REGIONS
-- A complex region can composed of boxes, sectors, chords, polygons and texts. 
-- It is implemented only in the Windows GDI, Windows GDI+, GDK, Cairo(*), and 
-- X-Windows base drivers.
--
-- Complex clipping regions can be created using
-- "begin_region"/(filled primtives)/.../"end_polygon". For more about
-- "begin_***" and "end_polygon" see Polygons.
--
-- Between a "begin_region" and a "end", all calls to "box", "sector", "chord", 
-- "begin_fill"/"vertex(x,y)"/.../"end_polygon" and Text will be composed in a 
-- region for clipping. This is the only exception when you can call a 
-- "begin_***" after another "begin_***".
--
-- When you call "begin_region" a new empty region will be created. So for the 
-- first operation you should use CD_UNION or CD_NOTINTERSECT combine modes. 
-- When you finished to compose the region call "end_polygon".
--
-- To make the region active you must call "clip_region". For other clipping 
-- regions see Clipping.
--
-- Although Cairo is capable of creating regions, you can not use them for 
-- clipping, just to use "is_point_in_region". And it can create only 
-- rectangle based regions, i.e. only "box" will actually combine rectangles 
-- into the region.
--
-- Complex clipping regions are not saved by "save_state".

inherit
	CANVAS_DRAW

feature {ANY}

	set_combine_mode (mode: STRING)
		-- Changes the way regions are combined when created. Values: CD_UNION, 
		-- CD_INTERSECT, CD_DIFFERENCE or CD_NOTINTERSECT. Default
		-- value: CD_UNION.
		require
			is_valid_combine_mode (mode)
		local
			i: INTEGER
		do
			if mode.is_equal("CD_UNION") then
				i := int_canvas_region_combine_mode (cnvs, 0)
			elseif mode.is_equal("CD_INTERSECT") then
				i := int_canvas_region_combine_mode (cnvs, 1)
			elseif mode.is_equal("CD_DIFFERENCE") then
				i := int_canvas_region_combine_mode (cnvs, 2)
			elseif mode.is_equal("CD_NOTINTERSECT") then
				i := int_canvas_region_combine_mode (cnvs, 3)
			end
		end

	get_combine_mode: STRING
		local
			i: INTEGER
		do
			i := int_canvas_region_combine_mode (cnvs, -1)

			if i.is_equal(0) then
				Result := "CD_UNION"
			elseif i.is_equal(1) then
				Result := "CD_INTERSECT"
			elseif i.is_equal(2) then
				Result := "CD_DIFFERENCE"
			elseif i.is_equal(3) then
				Result := "CD_NOTINTERSECT"
			else
				Result := "CD_UNION"
			end
		end

	set_combine_mode_union
		local
			i: INTEGER
		do
			i := int_canvas_region_combine_mode (cnvs, 0)
		end

	set_combine_mode_intersect
		local
			i: INTEGER
		do
			i := int_canvas_region_combine_mode (cnvs, 1)
		end

	set_combine_mode_difference
		local
			i: INTEGER
		do
			i := int_canvas_region_combine_mode (cnvs, 2)
		end

	set_combine_mode_not_intersect
		local
			i: INTEGER
		do
			i := int_canvas_region_combine_mode (cnvs, 3)
		end

	is_point_in_region (x, y: INTEGER): BOOLEAN
		-- Returns a non zero value if the point is contained inside the current 
		-- region.	
		local
			i: INTEGER
		do
			i := int_canvas_is_point_in_region (cnvs, x, y)

			if i.is_equal(0) then
				Result := False
			else
				Result := True
			end
		end

	wd_is_point_in_region (x, y: REAL_64): BOOLEAN
		-- Like "is_point_in_region" but for World coordinates.
		local
			i: REAL_64
		do
			i := int_wd_canvas_is_point_in_region (cnvs, x, y)

			if i.is_equal(0) then
				Result := False
			else
				Result := True
			end
		end

	offset_region (dx, dy: INTEGER)
		-- Moves the current region by the given offset. In X-Windows, if the 
		-- region moves to outside the canvas border, the part moved outside will 
		-- be lost, the region will need to be reconstructed.
		do
			int_canvas_offset_region (cnvs, dx, dy)
		end

	wd_offset_region (dx, dy: REAL_64)
		-- Like "offset_region" but for World coordinates.
		do
			int_wd_canvas_offset_region (cnvs, dx, dy)
		end

	get_region_box: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		-- Returns the rectangle of the bounding box of the current region.
		-- In the form (xmin, xmax, ymin, ymax)
		local
			xmin, xmax, ymin, ymax: INTEGER
		do
			int_canvas_get_region_box (cnvs, $xmin, $xmax, $ymin, $ymax)

			Result := [xmin, xmax, ymin, ymax]
		end

	wd_get_region_box: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
		-- Like "get_region_box" but with World coordinates.
		local
			xmin, xmax, ymin, ymax: REAL_64
		do
			int_wd_canvas_get_region_box (cnvs, $xmin, $xmax, $ymin, $ymax)

			Result := [xmin, xmax, ymin, ymax]
		end

	-- Verifications

	is_valid_combine_mode (mode: STRING): BOOLEAN
		do
			if mode.is_equal("CD_UNION") or
				mode.is_equal("CD_INTERSECT") or
				mode.is_equal("CD_DIFFERENCE") or
				mode.is_equal("CD_NOTINTERSECT") then
				Result := True
			else
				Result := False
			end
		end

feature {NONE}

	-- Internals

	int_canvas_region_combine_mode (wgt: POINTER; m: INTEGER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return cdCanvasRegionCombineMode ($wgt, $m);"
      end

	int_canvas_is_point_in_region (wgt: POINTER; x, y: INTEGER): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return cdCanvasIsPointInRegion ($wgt, $x, $y);"
      end

	int_wd_canvas_is_point_in_region (wgt: POINTER; x, y: REAL_64): INTEGER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return wdCanvasIsPointInRegion ($wgt, $x, $y);"
      end

	int_canvas_offset_region (wgt: POINTER; dx, dy: INTEGER)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"cdCanvasOffsetRegion ($wgt, $dx, $dy);"
      end

	int_wd_canvas_offset_region (wgt: POINTER; dx, dy: REAL_64)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"wdCanvasOffsetRegion ($wgt, $dx, $dy);"
      end

	int_canvas_get_region_box (wgt, c1, c2, c3, c4: POINTER)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"cdCanvasGetRegionBox ($wgt, $c1, $c2, $c3, $c4);"
      end

	int_wd_canvas_get_region_box (wgt, c1, c2, c3, c4: POINTER)
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"wdCanvasGetRegionBox ($wgt, $c1, $c2, $c3, $c4);"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
